import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from 'passport-jwt'
import { BaseService } from "@/base/base.service";


@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
  constructor(configService: ConfigService, private base: BaseService) {
    super({
      //解析用户提交的Bearer Token header数据
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      //加密码的secret
      secretOrKey: configService.get('TOKEN_SECRET')
    })
  }

  async validate({ sub: id }) {
    return this.base.adminUser.findUnique({
      where: {
        id
      }
    })
  }
}